package sample;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.concurrent.TimeUnit;
public class Snake{
    Magic m = new Magic();
    int distance = 0;
    public Snake(Yard y){
        this.y = y;
    }
    Boolean isMagic = false;


    Yard y = new Yard();
    Node head=new Node(60,200,new Node(60,210,new Node(60,220,null)));


    class Node{
        int x;
        int y;
        Direction dir = Direction.UP;
        Node next;
        void draw(Canvas canvas){
            GraphicsContext gc = canvas.getGraphicsContext2D();
            gc.setFill(Color.BLUE);
            gc.fillRect(x,y,Yard.BLOCK_SIZE,Yard.BLOCK_SIZE);
        }

        public Node(int x, int y,Node next) {
            this.x = x;
            this.y = y;
            this.next = next;
            }
        }


        public void draw(Canvas canvas) throws InterruptedException {
            for(Node n = head;n!=null;n=n.next){
                n.draw(canvas);
            }
            move();
        }
        public  void eat(Food f) throws InterruptedException {
            if(f.x==Yard.s.head.x&&f.y==Yard.s.head.y) {
                f.reAppear();
                y.score+=5;
                switch (head.dir) {
                    case UP:
                        Node newHeadu = new Node(head.x, head.y - Yard.BLOCK_SIZE, head);
                        newHeadu.dir = head.dir;
                        head = newHeadu;
                        TimeUnit.MILLISECONDS.sleep(100);
                        break;
                    case DOWN:
                        Node newHeadd = new Node(head.x, head.y + Yard.BLOCK_SIZE, head);
                        newHeadd.dir = head.dir;
                        head = newHeadd;
                        TimeUnit.MILLISECONDS.sleep(100);
                        break;
                    case LEFT:
                        Node newHeadl = new Node(head.x - Yard.BLOCK_SIZE, head.y, head);
                        newHeadl.dir = head.dir;
                        head = newHeadl;
                        TimeUnit.MILLISECONDS.sleep(100);
                        break;
                    case RIGHT:
                        Node newHeadr = new Node(head.x + Yard.BLOCK_SIZE, head.y, head);
                        newHeadr.dir = head.dir;
                        head = newHeadr;
                        TimeUnit.MILLISECONDS.sleep(100);
                        break;
                }
                }
            }

    private boolean isDead() {
        for(Node nx = head.next;nx!=null;nx = nx.next){
            if((head.x==nx.x&&head.y==nx.y)||head.x>(Yard.COL-1)*Yard.BLOCK_SIZE||head.y>(Yard.ROW-1)*Yard.BLOCK_SIZE||head.x<10
                    ||head.y<10){
                return true;
            }
            if((head.x>=Yard.COL/3*Yard.BLOCK_SIZE&&head.x<=(2*Yard.COL/3-1)*Yard.BLOCK_SIZE/10*10)&&(head.y==(Yard.ROW/3)*Yard.BLOCK_SIZE/10*10)){
                return true;
            }
            if((head.x>=Yard.COL/3*Yard.BLOCK_SIZE&&head.x<=(2*Yard.COL/3-1)*Yard.BLOCK_SIZE/10*10)&&(head.y==(2*Yard.ROW/3)*Yard.BLOCK_SIZE/10*10)){
                return true;
            }

        }
        return false;
    }

    public void eatMagic(Magic m){
        new Thread(new WaitThread(m,y.score)).start();
    }
    public  class WaitThread implements Runnable{
        Magic m;
        int score;
        public WaitThread(Magic m,int score){
            this.m = m;
            this.score = score;
        }
        public void run(){
            try{
                if(m.x==Yard.s.head.x&&m.y==Yard.s.head.y){
                    score+=5;
                    isMagic = true;

                    m.reAppear(score);
                    Thread.sleep(5000);
                    isMagic = false;
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


        private void move() throws InterruptedException {
                Node n = head;

            if(isDead()&&!isMagic){
                    y.flag = false;
                }

                switch (head.dir) {
                    case UP:
                       Node newHeadu = new Node(head.x, head.y-Yard.BLOCK_SIZE,head);
                       newHeadu.dir = head.dir;
                       for (; n.next.next != null; n = n.next);
                           n.next = null;
                       head = newHeadu;
                       distance++;
                       TimeUnit.MILLISECONDS.sleep(100);
                       break;
                   case DOWN:
                       Node newHeadd = new Node(head.x, head.y+Yard.BLOCK_SIZE,head);
                       newHeadd.dir = head.dir;
                      for (; n.next.next != null; n = n.next);
                           n.next = null;
                       head = newHeadd;
                       distance++;
                        TimeUnit.MILLISECONDS.sleep(100);
                        break;
                    case LEFT:
                        Node newHeadl = new Node(head.x-Yard.BLOCK_SIZE, head.y,head);
                        newHeadl.dir = head.dir;
                        for (; n.next.next != null; n = n.next);
                            n.next = null;
                        head = newHeadl;
                        distance++;
                        TimeUnit.MILLISECONDS.sleep(100);
                        break;
                    case RIGHT:
                        Node newHeadr = new Node(head.x+Yard.BLOCK_SIZE,head.y,head);
                        newHeadr.dir = head.dir;
                        for (; n.next.next != null; n = n.next);
                            n.next = null;
                        head = newHeadr;
                        distance++;
                        TimeUnit.MILLISECONDS.sleep(100);
                        break;
                }
    }


}



